<template>
	<view>
		<scroll-view @scrolltolower="scrolltolower" lower-threshold="10" :scroll-y="initialIsSticky" :style="{ height: props.scrollViewHeight + 'px' }">
			<view class="">
				<view class="tab">
					<view class="tab_flex">
						<view
							class="items"
							:class="index == data.active ? 'actitems' : ''"
							:style="{ width: itemWidth }"
							v-for="(item, index) in data.tabbarList"
							:key="index"
							@click="tabbarChange(item, index)"
						>
							{{ item.name }}
						</view>
					</view>
				</view>
				<view class="top_img flex_jc_cent" @click="$pageGo('/mypage/usertabbar/communitydraft')" v-if="data.active == 0">
					<view class="flex">
						<image src="/static/cg.png" mode=""></image>
						本地草稿
					</view>
				</view>
				<loadstatusVue :status="data.loadStatus">
					<view class="content" v-if="data.active < 3">
						<view class="take_item" @click="goInfo(item.up, item.self, item.createUserId, item.id)" v-for="(item, index) in data.list" :key="index">
							<view class="img_flex" v-if="item.createUser">
								<image class="uimg" v-if="item.createUser.img" :src="item.createUser.img" mode="aspectFill"></image>
								<image class="uimg" v-else src="https://www.daolangtongge.com/read-book-api/profile/mini_app/img/head.png" mode=""></image>
								<view class="img_right">
									<view class="take_user flex_jc_sb">
										<view class="u_l flex">
											<view class="flex_r">
												<view class="t1">{{ item.createUser.name }}</view>
												<view class="t2">{{ item.createTime }}</view>
											</view>
										</view>
										<view class="u_r flex_jc_cent" v-if="item.top == 1">
											<view class="flex">
												<image src="/static/tt.png" mode=""></image>
												<view class="">已置顶</view>
											</view>
										</view>
									</view>
									<view class="texts">
										<view class="title">{{ item.title }}</view>
										<view class="t_content">
											{{ item.content }}
										</view>
									</view>
									<view class="imglist" v-if="item.images.length != 0">
										<view class="img_item" v-for="(img, ind) in item.images.slice(0, 4)" :key="ind">
											<image :src="img" mode="aspectFill"></image>
											<view class="item_ads flex_jc_cent" v-if="ind == 3 && item.images.length > 4">{{ item.images.length - 4 }} +</view>
										</view>
									</view>
									<view class="" v-if="item.lastReply">
										<view class="comment">
											<view class="ts">
												<text>{{ item.lastReply.user.name }}：</text>
												{{ item.lastReply.content }}
											</view>
											<view class="c_imglist" v-if="item.lastReply.images">
												<view class="img_item" v-for="(img, ind) in item.lastReply.images.split(',').slice(0, 4)" :key="ind">
													<image :src="img" mode="aspectFill"></image>
												</view>
											</view>
										</view>
									</view>
									<view class="hd" v-if="item.activity">
										<image src="/static/q.png" mode=""></image>
										{{ item.activity.name }}
									</view>
								</view>
							</view>
							<view class="handle_block flex" v-if="data.active == 0" @click.stop="">
								<!-- self 1 0 -->
								<view class="switch-wrapper">
									<switch :checked="item.self == 1 ? true : false" @change="switchChange(item, $event)" color="#D8C6A7">
										<view style="margin-left: 10rpx">仅自己可见</view>
									</switch>
								</view>
								<!-- reply 1 0 -->
								<view class="switch-wrapper">
									<switch :checked="item.reply == 1 ? true : false" @change="switchChange1(item, $event)" color="#D8C6A7">
										<view style="margin-left: 10rpx">{{ item.reply == 1 ? '已开启评论' : '已关闭评论' }}</view>
									</switch>
								</view>
							</view>
						</view>
					</view>
					<view class="content" v-if="data.active == 3">
						<view
							class="take_item"
							@click="goInfo(item.up, item.article.self, item.article.createUserId, item.articleId)"
							v-for="(item, index) in data.list"
							:key="index"
						>
							<view class="img_flex" style="border: none" v-if="item.user">
								<image class="uimg" v-if="item.user.img" :src="item.user.img" mode="aspectFill"></image>
								<image class="uimg" v-else src="https://www.daolangtongge.com/read-book-api/profile/mini_app/img/head.png" mode=""></image>
								<view class="img_right">
									<!-- 我评论别人的内容   -->
									<view class="take_user flex_jc_sb">
										<view class="u_l flex">
											<view class="flex_r">
												<view class="t1">{{ item.user.name }}</view>
												<view class="t2">{{ item.createTime }}</view>
											</view>
										</view>
									</view>
									<!-- 文字 -->
									<view class="texts">
										<view class="take_text">{{ item.content }}</view>
									</view>
									<!-- 图片 -->
									<view class="imglist" v-if="item.images">
										<view class="img_item" v-for="(img, ind) in item.images.slice(0, 4)" :key="ind">
											<image :src="img" mode="aspectFill"></image>
											<view class="item_ads flex_jc_cent" v-if="ind == 3 && item.images.length > 4">{{ item.images.length - 4 }} +</view>
										</view>
									</view>

									<!-- 评论别人类型 -->

									<!-- 评论别人的恢复 -->
									<view class="" v-if="item.reply">
										<view class="comment">
											<view class="ts">
												<text>{{ item.reply.user.name }}：</text>
												{{ item.reply.content }}
											</view>
											<view class="c_imglist" v-if="item.reply.images">
												<view class="img_item" v-for="(img, ind) in item.reply.images.split(',').slice(0, 4)" :key="ind">
													<image :src="img" mode="aspectFill"></image>
												</view>
											</view>
										</view>
									</view>
									<view class="" v-else>
										<!-- 帖子的类型 -->
										<view class="take_img flex_jc_sb" v-if="item.article.images">
											<view class="flex">
												<image :src="item.article.images.split(',')[0]" mode="aspectFill"></image>
												<view class="flex_r">
													<view class="t1">{{ item.article.title }}</view>
													<view class="t2">#活动的名称</view>
												</view>
											</view>
											<uni-icons type="right" color="#979797"></uni-icons>
										</view>
										<view class="take_title flex_jc_sb" v-else>
											<view class="t_l">{{ item.article.title }}</view>
											<uni-icons type="right" color="#979797"></uni-icons>
										</view>
									</view>
									<!-- 删除帖子 和关闭评论优先 -->
									<!-- 	<view class="take_tips" v-if="item.article.reply == '0'">该帖子评论区已关闭</view>
									<view class="take_tips" v-if="item.article.self == '1'">该帖子已下架</view> -->
								</view>
							</view>
						</view>
					</view>
					<view class="content" v-if="data.active == 4">
						<view
							class="take_item"
							@click="goInfo(item.up, item.article.self, item.article.createUserId, item.articleId)"
							v-for="(item, index) in data.list"
							:key="index"
						>
							<view class="img_flex" style="border: none" v-if="item.createUser">
								<image class="uimg" v-if="item.createUser.img" :src="item.createUser.img" mode="aspectFill"></image>
								<image class="uimg" v-else src="https://www.daolangtongge.com/read-book-api/profile/mini_app/img/head.png" mode=""></image>
								<view class="img_right">
									<view class="take_user flex_jc_sb">
										<view class="u_l flex">
											<view class="flex_r">
												<view class="t1">{{ item.createUser.name }}</view>
												<view class="t2">{{ item.createTime }}</view>
											</view>
										</view>
									</view>
									<view class="texts">
										<view class="take_text">
											{{ item.content }}
										</view>
									</view>
									<view class="imglist" v-if="item.images">
										<view class="img_item" v-for="(img, ind) in item.images.slice(0, 4)" :key="ind">
											<image :src="img" mode="aspectFill"></image>
											<view class="item_ads flex_jc_cent" v-if="ind == 3 && item.images.length > 4">{{ item.images.length - 4 }} +</view>
										</view>
									</view>
									<!-- 评论别人类型 -->
									<!-- 删除帖子 和关闭评论优先 -->
									<view class="">
										<!-- 这是我回复别人的内容  -->
										<view class="comment" v-if="item.reply">
											<view class="ts">
												{{ item.reply.content }}
											</view>
											<view class="c_imglist" v-if="item.reply.images">
												<view class="img_item" v-for="(img, ind) in item.reply.images.split(',').slice(0, 4)" :key="ind">
													<image :src="img" mode="aspectFill"></image>
												</view>
											</view>
										</view>
										<view class="" v-else>
											<!-- 帖子的类型 -->
											<view class="take_img flex_jc_sb" v-if="item.article.images">
												<view class="flex">
													<image :src="item.article.images.split(',')[0]" mode="aspectFill"></image>
													<view class="flex_r">
														<view class="t1">{{ item.article.title }}</view>
														<view class="t2">#活动的名称</view>
													</view>
												</view>
												<uni-icons type="right" color="#979797"></uni-icons>
											</view>
											<view class="take_title flex_jc_sb" v-else>
												<view class="t_l">{{ item.article.title }}</view>
												<uni-icons type="right" color="#979797"></uni-icons>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</loadstatusVue>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, computed, onMounted } from 'vue';
import loadstatusVue from '@/components/loadstatus/loadstatus.vue';
import { likeArticleList, changeSelf, changeReply } from '@/api/community.js';
import { toast, showConfirm } from '@/utils/common.js';
const data = reactive({
	tabbarList: [
		{
			name: '发布',
			url: '/mini/article/self/article/list'
		},
		{
			name: '收藏',
			url: '/mini/article/self/like/article/list',
			type: 2
		},
		{
			name: '点赞',
			url: '/mini/article/self/like/article/list',
			type: 1
		},
		{
			name: '评论',
			url: '/mini/article/self/reply/list'
		},
		{
			name: '回复和赞',
			url: '/mini/article/self/reply/like/list'
		}
	],
	active: 0,
	list: [],
	loadStatus: 'before', // 加载状态
	hasMore: true, // 是否有更多数据
	isLoading: false, // 防止重复请求的锁
	queryParams: {
		type: undefined,
		url: '',
		pageNum: 1,
		pageSize: 10
	}
});
const itemWidth = computed(() => {
	return `${100 / data.tabbarList.length}%`;
});
const tabbarChange = (item, e) => {
	if (item.type) {
		data.queryParams.type = item.type;
	}
	data.active = e;
	data.queryParams.url = data.tabbarList[e].url;
	data.queryParams.pageNum = 1; // 重置页码
	getList(); // 重新加载
};
const props = defineProps({
	initialIsSticky: {
		type: Boolean,
		default: false
	},
	scrollViewHeight: {},
	userData: {}
});
const scrolltolower = () => {
	if (data.hasMore && (data.loadStatus === 'success' || data.loadStatus === 'noMore') && !data.isLoading) {
		data.queryParams.pageNum++;
		getList(true);
	}
};
// 延迟函数
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// 设置加载状态的方法
const setLoadStatus = (status, options = {}) => {
	data.loadStatus = status;
};
// 处理响应数据的通用方法
const handleResponseData = async (res, isLoadMore = false) => {
	// 如果是加载更多，确保loading状态至少显示500ms，避免闪烁
	if (isLoadMore) {
		await delay(500);
	}
	if (res.code == 200) {
		res.rows.forEach((item) => {
			if (item.images) {
				item.images = item.images.split(',');
			}
		});
		// 处理数据
		if (isLoadMore) {
			data.list.push(...res.rows);
		} else {
			data.list = res.rows || [];
		}
		// 判断是否有更多数据
		data.hasMore = res.rows && res.rows.length >= data.queryParams.pageSize;
		// 设置状态
		if (data.list.length === 0) {
			setLoadStatus('empty');
		} else if (!data.hasMore) {
			setLoadStatus('noMore');
		} else {
			setLoadStatus('success');
		}
		return true;
	}
	return false;
};
// 获取列表数据
const getList = async (isLoadMore = false) => {
	// 防止重复请求
	if (data.isLoading) return;
	try {
		data.isLoading = true;
		if (isLoadMore) {
			if (!data.hasMore) return;
			setLoadStatus('loading');
		} else {
			setLoadStatus('before');
			// 初始加载时显示loading至少1秒，避免闪烁
			await delay(300);
		}
		const res = await likeArticleList(data.queryParams);
		const success = await handleResponseData(res, isLoadMore);
		if (!success) {
			// 请求失败的处理
			setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
		}
	} catch (err) {
		await delay(300);
		setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
	} finally {
		data.isLoading = false;
	}
};
const switchChange = (item, e) => {
	const oldValue = item.self;
	const newValue = e.detail.value ? '1' : '0';
	item.self = newValue;
	changeSelf({ id: item.id }).then((res) => {
		if (res.code == 200) {
			toast(newValue === '1' ? '仅自己可见' : '所有人可见');
		}
	});
};
const switchChange1 = (item, e) => {
	const oldValue = item.reply;
	const newValue = e.detail.value ? '1' : '0';
	// 立即更新UI
	item.reply = newValue;
	changeReply({ id: item.id }).then((res) => {
		if (res.code == 200) {
			toast(newValue === '1' ? '已开启评论' : '已关闭评论');
		}
	});
};
// 点击进入详情
const goInfo = (up, self, userArticle, articleId) => {
	// 某一个帖子开启了仅自己观看
	// 如果是我我可以去看 不是我就提示
	let isMyself = userArticle == props.userData.id;
	if (up == 0) {
		toast('帖子已被平台下架');
		return;
	}
	if (self == 0) {
		uni.navigateTo({
			url: `/pages/community/info?id=${articleId}`
		});
	} else if (self == 1 && isMyself) {
		uni.navigateTo({
			url: `/pages/community/info?id=${articleId}`
		});
	} else {
		toast('帖子已下架');
	}
};
onMounted(() => {
	data.queryParams.url = data.tabbarList[data.active].url;
	getList();
});
</script>

<style lang="scss">
scroll-view ::-webkit-scrollbar {
	display: none !important;
	width: 0 !important;
	height: 0 !important;
	-webkit-appearance: none;
	background: transparent;
}
.tab {
	width: 750rpx;
	height: 72rpx;
	background: #f3f0e8;
	position: sticky;
	z-index: 4;
	top: 0%;
	.tab_flex {
		display: flex;
		height: 72rpx;
		align-items: center;
		.items {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #717171;
			text-align: center;
		}
		.actitems {
			font-weight: bold;
			font-size: 24rpx;
			color: #75613e;
		}
	}
}
.top_img {
	width: 710rpx;
	height: 80rpx;
	background: #ece6d5;
	border-radius: 20rpx;
	margin: 0rpx auto;
	image {
		width: 40rpx;
		height: 40rpx;
		margin-right: 20rpx;
	}
	font-family: PingFang SC, PingFang SC;
	font-weight: 500;
	font-size: 24rpx;
	color: #75613e;
}
.content {
	width: 100%;
	margin: 0rpx auto;
	position: relative;
	padding: 0rpx 20rpx;
	box-sizing: border-box;
	.take_item {
		width: 100%;
		background: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin: 20rpx auto;
		.img_flex {
			display: flex;
			width: 100%;
			.uimg {
				display: block;
				width: 60rpx;
				height: 60rpx;
				border-radius: 30rpx;
			}
		}
		.img_right {
			flex: 1;
			width: 0;
			margin-left: 14rpx;
			.take_user {
				.u_l {
					.flex_r {
						.t1 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 24rpx;
							color: #000000;
						}
						.t2 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 20rpx;
							color: #8f8f8b;
						}
					}
				}
				.u_r {
					width: 124rpx;
					height: 44rpx;
					background: #ffffff;
					border-radius: 22rpx;
					border: 1px solid #b19561;
					image {
						width: 24rpx;
						height: 24rpx;
						margin-right: 4rpx;
					}
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #b19561;
				}
			}
			.texts {
				.take_text {
					margin-top: 20rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #1b1b1b;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.title {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #1b1b1b;
					margin-top: 20rpx;
				}
				.t_content {
					margin-top: 20rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #1b1b1b;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 4; //几行就填几
					-webkit-box-orient: vertical;
					white-space: pre-wrap;
				}
			}

			.imglist {
				margin-top: 20rpx;
				display: flex;
				.img_item {
					width: 142rpx;
					height: 142rpx;
					margin-right: 14rpx;
					position: relative;
					image {
						width: 142rpx;
						height: 142rpx;
						border-radius: 12rpx;
					}
				}
				.item_ads {
					width: 142rpx;
					height: 142rpx;
					position: absolute;
					top: 0%;
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #ffffff;
					background: rgba(0, 0, 0, 0.5);
					border-radius: 12rpx;
				}
			}
			.take_tips {
				margin-top: 20rpx;
				width: 596rpx;
				height: 70rpx;
				background: #f7f7f7;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #1b1b1b;
				padding: 0rpx 20rpx;
				box-sizing: border-box;
				line-height: 70rpx;
			}
			.take_title {
				margin-top: 20rpx;
				width: 596rpx;
				height: 70rpx;
				background: #f7f7f7;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				padding: 0rpx 20rpx;
				box-sizing: border-box;
				line-height: 70rpx;
				.t_l {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #1b1b1b;
					flex: 1;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}
			.take_img {
				margin: 20rpx auto;
				width: 596rpx;
				background: #f7f7f7;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				padding: 20rpx;
				box-sizing: border-box;
				image {
					width: 80rpx;
					height: 80rpx;
					background: #dfdfdf;
					border-radius: 8rpx;
				}
				.flex_r {
					margin-left: 16rpx;
					.t1 {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						color: #1b1b1b;
					}
					.t2 {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 20rpx;
						color: #979797;
						margin-top: 15rpx;
					}
				}
			}
			.comment {
				margin: 20rpx auto;
				background: #f7f7f7;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				padding: 18rpx 20rpx;
				box-sizing: border-box;
				.ts {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #1b1b1b;
					text {
						color: #979797;
					}
				}
				.c_imglist {
					margin-top: 20rpx;
					display: flex;
					.img_item {
						width: 130rpx;
						height: 130rpx;
						margin-right: 12rpx;
						position: relative;
						image {
							width: 130rpx;
							height: 130rpx;
							border-radius: 12rpx;
						}
					}
					.item_ads {
						width: 130rpx;
						height: 130rpx;
						position: absolute;
						top: 0%;
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 28rpx;
						color: #ffffff;
						background: rgba(0, 0, 0, 0.5);
						border-radius: 12rpx;
					}
				}
			}

			.hd {
				margin: 20rpx 0rpx;
				height: 52rpx;
				background: #eee8d8;
				border-radius: 80rpx 80rpx 80rpx 80rpx;
				padding: 0rpx 24rpx;
				line-height: 52rpx;
				box-sizing: border-box;
				display: inline-block;
				image {
					width: 28rpx;
					height: 28rpx;
					margin-right: 10rpx;
					vertical-align: middle;
				}
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #75613e;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
		.handle_block {
			margin-top: 20rpx;
			border-top: 1px solid #ededed;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #75613e;
		}
	}
	.takes_item {
	}
}
.switch-wrapper {
	/* 调整开关整体容器的对齐方式（可选） */
	display: flex;
	/* uni-app文字类名 */
	font-size: 36rpx !important; /* 固定文字大小 */
	transform: none !important; /* 取消文字继承的缩放 */
	/* 深度选择器穿透原生组件样式 */
	::v-deep wx-switch, /* 微信小程序 */
	::v-deep uni-switch {
		/* 其他平台 */
		/* 缩小开关的整体尺寸（通过transform缩放） */
		transform: scale(0.6); /* 0.8表示缩小到80%，可根据需要调整 */
		transform-origin: left center; /* 缩放原点左对齐，避免文字偏移 */
		display: flex;
		align-items: center;
	}
}
</style>
